package com.lottery.shadowbd.ui.fragment;


import android.content.Intent;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;

import com.lottery.shadowbd.R;
import com.lottery.shadowbd.ui.activity.LotteryDrawActivity;
import com.lottery.shadowbd.ui.activity.MainActivity;
import com.lottery.shadowbd.adapter.LotteryDrawAdapter;
import com.lottery.shadowbd.bean.Lottery;
import com.lottery.shadowbd.bean.ResultBean;
import com.lottery.shadowbd.callback.ECallback;
import com.lottery.shadowbd.callback.OnMyItemClickListener;
import com.lottery.shadowbd.io.api.LotApiIO;
import com.lottery.shadowbd.io.api.WebApiIO;
import com.lottery.shadowbd.support.Constant;
import com.lottery.shadowbd.support.SpacesItemDecoration;
import com.lottery.shadowbd.tuple.Tuple2;
import com.lottery.shadowbd.utils.ToastUtil;
import com.lottery.shadowbd.widget.PullLoadMoreRecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * 开奖
 */
public class LotteryDrawFragment extends BaseFragment implements PullLoadMoreRecyclerView.PullLoadMoreListener, OnMyItemClickListener {

    private View mView;

    private PullLoadMoreRecyclerView pull_load_lot;

    private LotteryDrawAdapter mLotAdapter;

    private List<Lottery.IEntity> mList;

    @Override
    protected View initView(LayoutInflater inflater) {
        mView = inflater.inflate(R.layout.fragment_lottery_draw, null);

        pull_load_lot = (PullLoadMoreRecyclerView) mView.findViewById(R.id.pull_load_lot);

        return mView;
    }

    @Override
    protected void initData() {
        mContext = getActivity();

        mList = new ArrayList<>();

        mLotAdapter = new LotteryDrawAdapter(mContext, mList);

        pull_load_lot.setLinearLayout();
        pull_load_lot.addItemDecoration(new SpacesItemDecoration(5));

        pull_load_lot.setAdapter(mLotAdapter);

        getLot();
        //getWebUrl();
    }

    @Override
    protected void listener() {
        pull_load_lot.setmPullLoadMoreListener(this);
        mLotAdapter.setMyItemClickListener(this);
    }

    @Override
    protected void widgetClick(View v) {

    }

    private void getLot() {
        if(!pull_load_lot.isRefresh()) {
            showLoadingDialog();
        }

        LotApiIO.getInstance().getLotList("1458219747840", new ECallback<List<Lottery.IEntity>, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(List<Lottery.IEntity> res) {
                dismissDialog();
                if(pull_load_lot.isRefresh()) {
                    pull_load_lot.setRefreshing(false);
                }

                mList.clear();

                mList.addAll(res);

                mLotAdapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                dismissDialog();
                if(pull_load_lot.isRefresh()) {
                    pull_load_lot.setRefreshing(false);
                }
            }

            @Override
            public void onThrowable(Throwable e) {
                dismissDialog();
                if(pull_load_lot.isRefresh()) {
                    pull_load_lot.setRefreshing(false);
                }
                ToastUtil.showShort(mContext, "网络请求错误!");
            }
        });
    }

    private void getWebUrl() {
        showLoadingDialog();

        WebApiIO.getInstance().getWebUrl(Constant.myAppID, new ECallback<ResultBean, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(ResultBean res) {
                dismissDialog();
                if ("1".equals(res.getIsshowwap())) {
                    if (getActivity() != null) {
                        Intent mIntent = new Intent(getActivity(), MainActivity.class);
                        mIntent.putExtra(Constant.KeyWebUrl, res.getWapurl());
                        startActivity(mIntent);
                        //getActivity().finish();
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                dismissDialog();
                ToastUtil.showShort(mContext, error._2);
            }

            @Override
            public void onThrowable(Throwable e) {
                dismissDialog();
                ToastUtil.showShort(mContext, "网络错误!");
            }
        });
    }

    @Override
    public void onRefresh() {
        getLot();
    }

    @Override
    public void onLoadMore() {

    }

    @Override
    public void onItemClick(View view, int position) {
        if (mList != null && position <= mList.size()) {
            switch (view.getId()) {
                case R.id.bannerId:
                    getWebUrl();
                    break;
                default:
                    Lottery.IEntity entity = mList.get(position - 1);

                    Intent mIntent = new Intent(mContext, LotteryDrawActivity.class);

                    mIntent.putExtra("name", entity.getLotName());

                    startActivity(mIntent);
                    break;
            }
        }
    }
}
